package chances.epg.data.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.SpringWebUtils;
import chances.epg.data.service.BizPositionService;
import chances.epg.data.service.BizService;
import chances.epg.data.service.ChannelService;
import chances.epg.data.service.ContentService;
import chances.epg.entity.biz.Biz;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;
import chances.epg.navigator.service.NavigateUrlFactory;

public abstract class QueryTag extends BodyTagSupport {
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 4632336326805632444L;

	protected String scope = PageContext.REQUEST;
	protected String var = "var";

	protected NavigateUrlFactory navigateUrlFactory;

	@Override
	public void setPageContext(PageContext pageContext) {
		super.setPageContext(pageContext);

		navigateUrlFactory = SpringWebUtils.findBean(pageContext, NavigateUrlFactory.class);
	}

	/**
	 * 
	 * @return
	 */
	protected BizService getBizService() {
		return (BizService) this.getBean("bizService");
	}

	/**
	 * 
	 * @return
	 */
	protected ContentService getContentService() {
		return (ContentService) this.getBean("contentService");
	}

	/**
	 * 
	 * @return
	 */
	protected ChannelService getChannelService() {
		return (ChannelService) this.getBean("channelService");
	}

	/**
	 * 
	 * @return
	 */
	protected BizPositionService getBizPositionService() {
		return (BizPositionService) this.getBean("bizPositionService");
	}

	/**
	 * 当前展示栏目的编码.
	 * 
	 * @return
	 */
	protected String getCurBizCode() {
		AccessContext context = AccessContextUtils.getAccessContext(pageContext);
		if (context != null) {
			return context.getBizCode();
		} else {
			return "";
		}
	}

	/**
	 * 当前的展示栏目。
	 * 
	 * @return
	 */
	protected Biz getCurBiz() {
		AccessContext context = AccessContextUtils.getAccessContext(pageContext);
		if (context != null) {
			return context.getBiz();
		} else {
			return null;
		}
	}

	/**
	 * 当前展示栏目对应的编排栏目.
	 * 
	 * @return
	 */
	protected String getCurCategoryCode() {
		AccessContext context = AccessContextUtils.getAccessContext(pageContext);
		if (context != null) {
			Biz curBiz = context.getBiz();
			return curBiz.getCategoryCode();
		} else {
			return "";
		}
	}

	/**
	 * 
	 * @param value
	 */
	protected void setResult(Object value) {
		this.setAttribute(this.getVar(), value);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected void setAttribute(String name, Object value) {
		if (StringUtils.isEmpty(name) || value == null)
			return;

		int nScope = PageContext.REQUEST_SCOPE;
		if (StringUtils.isNotEmpty(scope)) {
			nScope = pageContext.getAttributesScope(scope);
			nScope = nScope == 0 ? PageContext.REQUEST_SCOPE : nScope;
		}
		pageContext.setAttribute(name, value, nScope);
	}

	/**
	 * Get spring bean
	 * 
	 * @param beanName
	 * @return
	 */
	protected Object getBean(String beanName) {
		return SpringWebUtils.findBean(pageContext, beanName);
	}

	/**
	 * 
	 * @return
	 */
	protected HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) pageContext.getRequest();
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
}
